<?php
session_start ();
require_once ("../../commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');

$_SESSION ['pathway'] = '../..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include_once ($_SESSION ['pathway'] . "/header.php");

if (isset ( $_GET ['action'] )) {
	$action = $_GET ['action'];
}
if (isset ( $_GET ['index'] )) {
	$index = $_GET ['index'];
}

foreach ( $_POST as $key => $value ) {
	if ($key != '') {
		$value = mysql_real_escape_string ( $value );
		// ~ $value = mysqli_real_escape_string($link,$value);
		$value = str_replace ( ',', '.', $value );
		$_POST [$key] = $value;
	}
}

$allfichiers = '';
if (isset ( $_POST ['code1'] )) {
	$code1 = strtoupper ( $_POST ['code1'] );
} else {
	$code1 = '';
}
if (isset ( $_POST ['code2'] )) {
	$code2 = strtoupper ( $_POST ['code2'] );
} else {
	$code2 = '';
}
if (isset ( $_POST ['code3'] )) {
	$code3 = strtoupper ( $_POST ['code3'] );
} else {
	$code3 = '';
}
if (isset ( $_POST ['code4'] )) {
	$code4 = strtoupper ( $_POST ['code4'] );
} else {
	$code4 = '';
}
if (isset ( $_POST ['code5'] )) {
	$code5 = strtoupper ( $_POST ['code5'] );
} else {
	$code5 = '';
}
if (isset ( $_POST ['code5'] )) {
	$code5 = $_POST ['code5'];
	$table_individus_wholegenomeanalysis = explode ( ';', $code5 );
} else {
	$code5 = '';
}
if (isset ( $_POST ['codeexterne'] )) {
	$codeexterne = $_POST ['codeexterne'];
	$table_individus_codeexterne = explode ( ';', $codeexterne );
} else {
	$codeexterne = '';
}
if (isset ( $_POST ['codegenethon'] )) {
	$codegenethon = $_POST ['codegenethon'];
	$table_individus_codeexterne = explode ( ';', $codegenethon );
} else {
	$codegenethon = '';
}
if (isset ( $_POST ['centre'] )) {
	$centre = $_POST ['centre'];
	$centre = strtoupper ( $centre );
} else {
	$centre = '';
}
if (isset ( $_POST ['neurologist'] )) {
	$neurologist = $_POST ['neurologist'];
	$neurologist = strtoupper ( $neurologist );
} else {
	$neurologist = '';
}
if (isset ( $_POST ['techniquewholegenomeanalysis'] )) {
	$techniquewholegenomeanalysis = $_POST ['techniquewholegenomeanalysis'];
} else {
	$techniquewholegenomeanalysis = '';
}
if (isset ( $_POST ['commentairetechnique'] )) {
	$commentairetechnique = $_POST ['commentairetechnique'];
} else {
	$commentairetechnique = '';
	$commentairetechnique = mysql_real_escape_string ( $commentairetechnique );
}
// ~ if(isset($_POST['commentairetechnique'])){$commentairetechnique = $_POST['commentairetechnique'];}else{$commentairetechnique = '';$commentairetechnique = mysqli_real_escape_string($link,$commentairetechnique);}
if (isset ( $_POST ['materialwholegenomeanalysis'] )) {
	$materialwholegenomeanalysis = $_POST ['materialwholegenomeanalysis'];
	$materialwholegenomeanalysis = mysql_real_escape_string ( $materialwholegenomeanalysis );
} else {
	$materialwholegenomeanalysis = '';
}
// ~ if(isset($_POST['materialwholegenomeanalysis'])){ $materialwholegenomeanalysis = $_POST['materialwholegenomeanalysis']; $materialwholegenomeanalysis = mysqli_real_escape_string($link,$materialwholegenomeanalysis);}else{ $materialwholegenomeanalysis = ''; }
if (isset ( $_POST ['variants'] )) {
	$variants = $_POST ['variants'];
} else {
	$variants = '';
}
if (isset ( $_POST ['commentairewholegenomeanalysis'] )) {
	$commentairewholegenomeanalysis = $_POST ['commentairewholegenomeanalysis'];
	$commentairewholegenomeanalysis = mysql_real_escape_string ( $commentairewholegenomeanalysis );
} else {
	$commentairewholegenomeanalysis = '';
}
// ~ if(isset($_POST['commentairewholegenomeanalysis'])){ $commentairewholegenomeanalysis = $_POST['commentairewholegenomeanalysis']; $commentairewholegenomeanalysis = mysqli_real_escape_string($link,$commentairewholegenomeanalysis);}else{ $commentairewholegenomeanalysis = ''; }
if (isset ( $_POST ['date'] )) {
	$date = $_POST ['date'];
} else {
	$date = '';
}
if (isset ( $_POST ['statut'] )) {
	$statut = $_POST ['statut'];
} else {
	$statut = '';
}
if (isset ( $_POST ['user'] )) {
	$user = strtoupper ( $_POST ['user'] );
} else {
	$user = '';
}
$last_modifier = strtoupper ( $_SESSION ['id'] ); // dès qu'on soumet le formulaire, le last_modifier est actualisé;}
if (isset ( $_POST ['diagnosticwholegenomeanalysis'] )) {
	$diagnosticwholegenomeanalysis = $_POST ['diagnosticwholegenomeanalysis'];
	$diagnosticwholegenomeanalysis = mysql_real_escape_string ( $diagnosticwholegenomeanalysis );
} else {
	$diagnosticwholegenomeanalysis = '';
}
// ~ if(isset($_POST['diagnosticwholegenomeanalysis'])){ $diagnosticwholegenomeanalysis = $_POST['diagnosticwholegenomeanalysis']; $diagnosticwholegenomeanalysis = mysqli_real_escape_string($link,$diagnosticwholegenomeanalysis);}else{ $diagnosticwholegenomeanalysis = ''; }
if (isset ( $_POST ['validation'] )) {
	$validation = $_POST ['validation'];
} else {
	$validation = '';
}
if (isset ( $_POST ['afaire'] )) {
	$afaire = $_POST ['afaire'];
	$afaire = mysql_real_escape_string ( $afaire );
} else {
	$afaire = '';
}
// ~ if(isset($_POST['afaire'])){ $afaire = $_POST['afaire']; $afaire = mysqli_real_escape_string($link,$afaire);}else{ $afaire = ''; }
if (isset ( $_POST ['commentairesconclusion'] )) {
	$commentairesconclusion = $_POST ['commentairesconclusion'];
	$commentairesconclusion = mysql_real_escape_string ( $commentairesconclusion );
} else {
	$commentairesconclusion = '';
}
// ~ if(isset($_POST['commentairesconclusion'])){ $commentairesconclusion = $_POST['commentairesconclusion']; $commentairesconclusion = mysqli_real_escape_string($link,$commentairesconclusion);}else{ $commentairesconclusion = ''; }
if (isset ( $_POST ['fichiersattaches'] ) and $_POST ['fichiersattaches'] != '') {
	$fichiersattaches = $_POST ['fichiersattaches'];
} else {
	$fichiersattaches = '';
}

$allfichiers .= $fichiersattaches;

if (isset ( $_FILES ['fichieraajouter'] ) and $_FILES ['fichieraajouter'] ['error'] == 0) {
	$tabledate = getdate ();
	$id_unique = $tabledate ['seconds'] . $tabledate ['minutes'] . $tabledate ['hours'] . $tabledate ['mday'] . $tabledate ['mon'] . $tabledate ['year'];
	
	move_uploaded_file ( $_FILES ['fichieraajouter'] ['tmp_name'], '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) );
	chmod ( '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ), 0777 );
	$allfichiers .= $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) . ";";
}
// ~ echo "fichieraajouter ".$_FILES['fichieraajouter']['name']."</br>";
// ~ echo "fichiersattaches $fichiersattaches</br>";
// ~ echo "allfichiers $allfichiers</br>";

if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'edit') {
	$update = "UPDATE `$db`.`wholegenomeanalysis` SET 
	`code1` = '$code1',
	`code2` = '$code2',
	`code3` = '$code3',
	`code4` = '$code4',
	`code5` = '$code5',
	`codeexterne` = '$codeexterne',
	`codegenethon` = '$codegenethon',
	`centre` = '$centre',
	`neurologist` = '$neurologist',
	`techniquewholegenomeanalysis` = '$techniquewholegenomeanalysis',
	`commentairetechnique` = '$commentairetechnique',
	`materialwholegenomeanalysis` = '$materialwholegenomeanalysis',
	`fichiersattaches` = '$allfichiers',
	`variants` = '$variants',
	`commentairewholegenomeanalysis` = '$commentairewholegenomeanalysis',
	`date` = '$date',
	`statut` = '$statut',
	`user` = '$user',
	`last_modifier` = '$last_modifier',
	`diagnosticwholegenomeanalysis` = '$diagnosticwholegenomeanalysis',
	`validation` = '$validation',
	`afaire` = '$afaire',
	`commentairesconclusion` = '$commentairesconclusion'
	WHERE `wholegenomeanalysis`.`index` = '$index';";
	// ~ echo $update;
	mysql_query ( mysql_real_escape_string($update) ) or die ( "Can't modify entry : " . mysql_error () );
	// ~ mysqli_query($link,$update) or die("Can't modify entry : " . mysql_error());
} else if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'insert') {
	
	$nombre_individus = count ( $table_individus_wholegenomeanalysis );
	for($i = 0; $i < $nombre_individus; $i ++) {
		$recupereindexmaxwholegenomeanalysis = "select MAX(`index`) FROM `wholegenomeanalysis`";
		$resultindex = mysql_query ( $recupereindexmaxwholegenomeanalysis ) or die ( "Impossible de r&eacute;cup&eacute;rer l'index de la table wholegenomeanalysis" );
		// ~ $resultindex = mysqli_query($link,$recupereindexmaxwholegenomeanalysis) or die ("Impossible de r&eacute;cup&eacute;rer l'index de la table wholegenomeanalysis");
		while ( $ligne = mysql_fetch_row ( $resultindex ) ) {
			// ~ while ($ligne = mysqli_fetch_row($resultindex)) {
			$indexwholegenomeanalysis = $ligne [0] + 1;
		}
		
		$code5 = $table_individus_wholegenomeanalysis [$i];
		$codeexterne = $table_individus_codeexterne [$i];
		$insertwholegenomeanalysis = "INSERT INTO `$db`.`wholegenomeanalysis` (
		`index`, 
		`code1`,
		`code2`,
		`code3`,
		`code4`,
		`code5`,
		`codeexterne`,
		`codegenethon`,
		`centre`,
		`neurologist`,
		`techniquewholegenomeanalysis`,
		`commentairetechnique`,
		`materialwholegenomeanalysis`,
		`fichiersattaches`,
		`variants`,
		`commentairewholegenomeanalysis`,
		`date`,
		`statut`,
		`user`,
		`last_modifier`,
		`diagnosticwholegenomeanalysis`,
		`validation`,
		`afaire`,
		`commentairesconclusion`) VALUES (
		'$indexwholegenomeanalysis',
		'$code1',
		'$code2',
		'$code3',
		'$code4',
		'$code5',
		'$codeexterne',
		'$codegenethon',
		'$centre',
		'$neurologist',
		'$techniquewholegenomeanalysis',
		'$commentairetechnique',
		'$materialwholegenomeanalysis',
		'$allfichiers',
		'$variants',
		'$commentairewholegenomeanalysis',
		'$date',
		'$statut',
		'$user',
		'$last_modifier',
		'$diagnosticwholegenomeanalysis',
		'$validation',
		'$afaire',
		'$commentairesconclusion');";
		mysql_query ( mysql_real_escape_string($insertwholegenomeanalysis) ) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table wholegenomeanalysis : " . mysql_error () );
		// ~ mysqli_query($link,$insertwholegenomeanalysis) or die("Impossible d'ins&eacute;rer donn&eacute;es dans table wholegenomeanalysis : " . mysql_error());
		// ~ echo $insertwholegenomeanalysis;
	}
}

if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'upload') {
	$_POST = array ();
	echo "<script>	window.location.href=\"../wholegenomeanalysis.php?action=edit&index=" . $index . "&table=wholegenomeanalysis\"</script>";
} else if (isset ( $_POST ['submitprinc'] ) and $_POST ['submitprinc'] == 'Ok') {
	$_POST = array ();
	echo "<script>	window.location.href=\"./../../start.php\"</script>";
} else {
	$_POST = array ();
	echo "<script>	window.location.href=\"../wholegenomeanalysis.php?action=edit&index=" . $index . "&table=wholegenomeanalysis\"</script>";
}

?>


